﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MxTol.Models;
using MxTol.Managers.Helpers;

namespace MxTol.Managers
{
    /// <summary>
    /// 管理员管理器
    /// </summary>
    public class AdminManager
    {
        /// <summary>
        /// 获取所有管理员(Json分页格式)
        /// </summary>
        public static string GetAllAdmin(int currentPage, int pageSize)
        {
            string json = string.Empty;

            try
            {
                string sql = "select * from Admin;";
                json = PaginationHelper.GetJsonBySql(currentPage, pageSize, sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return json;
        }

        /// <summary>
        /// 获取所有管理员(Json分页格式)
        /// </summary>
        public static string GetAllAdmin(int currentPage, int pageSize, string sql)
        {
            string json = string.Empty;

            try
            {
                json = PaginationHelper.GetJsonBySql(currentPage, pageSize, sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return json;
        }

        /// <summary>
        /// 判断指定账号是否存在
        /// </summary>
        public static bool IsAccountExsit(string account)
        {
            bool isExist = false;
            try
            {
                string sql = string.Format("select count(id) from Admin where account = '{0}';", account);
                int count = int.Parse(SqlHelper.ExecuteScalar(ManagerConfig.ConnectionString, sql).ToString());
                if (count != 0)
                {
                    isExist = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return isExist;
        }

        /// <summary>
        /// 判断密码和账号是否匹配
        /// </summary>
        public static bool IsPswordMatch(string account, string psword)
        {
            bool isMatch = false;
            try
            {
                string sql = string.Format("select count(id) from Admin where account = '{0}' and  psword = '{1}';", account, psword.ToLower());
                int count = int.Parse(SqlHelper.ExecuteScalar(ManagerConfig.ConnectionString, sql).ToString());
                if (count != 0)
                {
                    isMatch = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return isMatch;
        }

        /// <summary>
        /// 判断指定姓名的管理员是否存在
        /// </summary>
        public static bool IsNameExist(string name)
        {
            bool isExist = false;

            try
            {
                string sql = string.Format("select count(id) from Admin where name = '{0}';", name);
                int count = int.Parse(SqlHelper.ExecuteScalar(ManagerConfig.ConnectionString, sql).ToString());
                if (count != 0)
                {
                    isExist = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return isExist;
        }

        /// <summary>
        /// 判断指定号码的管理员是否存在
        /// </summary>
        public static bool IsPhoneExist(string phone)
        {
            bool isExist = false;
            try
            {
                string sql = string.Format("select count(id) from Admin where phone = '{0}';", phone);
                int count = int.Parse(SqlHelper.ExecuteScalar(ManagerConfig.ConnectionString, sql).ToString());
                if (count != 0)
                {
                    isExist = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return isExist;
        }

        /// <summary>
        /// 根据账号获取管理员级别  0：普通管理员  1：超级管理员  2：系统管理员
        /// </summary>
        public static int GetAdminLevel(string account)
        {
            int level = 0;
            try
            {
                string sql = string.Format("select level from Admin where account = '{0}';", account);
                level = int.Parse(SqlHelper.ExecuteScalar(ManagerConfig.ConnectionString, sql).ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return level;
        }

        /// <summary>
        /// 根据账号获取管理员姓名
        /// </summary>
        public static string GetAdminName(string account)
        {
            string name = string.Empty;
            try
            {
                string sql = string.Format("select name from Admin where account = '{0}';", account);
                name = SqlHelper.ExecuteScalar(ManagerConfig.ConnectionString, sql).ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return name;
        }
    }
}
